﻿using HandyControl.Controls;
using LYR_RPA.dialog;
using LYR_RPA.models.project;
using LYR_RPA.utils;
using System;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;


namespace LYR_RPA.pages.home
{
    /// <summary>
    /// ExecutePage.xaml 的交互逻辑
    /// </summary>
    public partial class ExecutePage : Page
    {
        public ExecutePage()
        {
            InitializeComponent();
            Executetems.ItemsSource = CommonUtils.ExecuteCollections;
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            var button = sender as Button;
            if (button.DataContext is Execute data)
            {
                if (data.Processing != null && !data.Processing.HasExited)
                {
                    CommonUtils.CloseProcess("UiPath.Executor");
                }
                data.IsCancel = true;
                SnackbarThree.Background = Brushes.Red;
                var messageQueue = SnackbarThree.MessageQueue;
                // 显示消息
                var message = "取消执行";
                if (!CommonUtils.ExecuteCollections.Remove(data))
                {
                    // 显示消息
                    message = "取消执行失败";
                }
                messageQueue.Enqueue(message);
                CommonUtils.IsExecute = false;
                CommonUtils.AddExecuteTask(null);
            }

        }

        private void ClearExecute_Click(object sender, RoutedEventArgs e)
        {
            int count = 0;
            while (CommonUtils.ExecuteCollections.Count > count)
            {
                var execute= CommonUtils.ExecuteCollections[count];
                if (execute.Processing == null || execute.Processing.HasExited)
                {
                    CommonUtils.ExecuteCollections.Remove(execute);
                }
                else
                {
                    count++;
                }
                
            }
            CommonUtils.IsExecute = false;
            SnackbarThree.Background = Brushes.Orange;
            var messageQueue = SnackbarThree.MessageQueue;
            
            // 显示消息
            var message = "清除完毕！";
            messageQueue.Enqueue(message);
        }

        private void StartTask_Click(object sender, RoutedEventArgs e)
        {
             foreach (var data in CommonUtils.ExecuteCollections)
            {
                data.IsTask = false;
            }
            Task.Run(() =>
            {
                CommonUtils.AddExecuteTask(null);
            });
        }

        private void Timing_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            Execute execute = button.DataContext as Execute;
            TimeDialog timeDialog=new TimeDialog(execute);
            //创建弹窗标题
            System.Windows.Window window = new System.Windows.Window
            {
                Content = timeDialog,
                WindowStyle = WindowStyle.None,
                SizeToContent = SizeToContent.WidthAndHeight,  // 设置为 Manual
                MinHeight = 250,  // 指定宽度
                MinWidth = 300,  // 指定高度
                MaxWidth = 600,
                MaxHeight = 600,
                ShowInTaskbar = false,
                ResizeMode = ResizeMode.NoResize,
                WindowState = WindowState.Normal,
                AllowsTransparency = true,
                WindowStartupLocation = WindowStartupLocation.CenterScreen,
                Background = Brushes.Transparent,
            };
            //窗口焦点
            window.Activate();
            //置顶
            window.Topmost = true;
            window.ShowDialog();
            SnackbarThree.Background = Brushes.Green;
            var messageQueue = SnackbarThree.MessageQueue;
            // 显示消息
            var message = "时间选择完毕需点上方开始执行";
            messageQueue.Enqueue(message);
        }

        private void CancelAll_Click(object sender, RoutedEventArgs e)
        {
            while (CommonUtils.ExecuteCollections.Count > 0)
            {
                var execute = CommonUtils.ExecuteCollections[0];
                if (execute.Processing != null && !execute.Processing.HasExited)
                {
                    CommonUtils.CloseProcess("UiPath.Executor");
                }
                CommonUtils.ExecuteCollections.Remove(execute);
            }
            CommonUtils.IsExecute = false;
            SnackbarThree.Background = Brushes.Orange;
            var messageQueue = SnackbarThree.MessageQueue;

            // 显示消息
            var message = "清除完毕！";
            messageQueue.Enqueue(message);
        }
    }
}
